package com.yanqu.road.entity.cricket;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.cricket.config.*;
import com.yanqu.road.entity.cricket.helper.CricketFightForcePro;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 蛐蛐系统配置
 */
public class CricketSystemConfig {

    /**
     * 蛐蛐系统气势参数：怯场参数k1，k2，投降参数n1，n2
     */
    private CricketFightForcePro cricketFightForcePro;

    /**
     * 蛐蛐列表
     */
    private Map<Integer, CricketConfig> cricketConfigMap;

    /**
     * 蛐蛐升品列表
     */
    private Map<Integer, CricketUpConfig> cricketUpConfigMap;

    /**
     * 场景列表
     */
    private Map<Integer, CricketScenceConfig> cricketScenceConfigMap;


    /**
     * 蛐蛐竞技场表
     */
    private Map<Integer, CricketCourtConfig> cricketCourtConfigMap;

    /**
     * 战果分计算 （根据胜负场次配置 3:0|2:1|1:2|0:3 ）
     */
    private List<Integer> cricketFightScoreAttack = new ArrayList<>();

    private List<Integer> cricketFightScoreDefend = new ArrayList<>();

    /**
     * 竞技场活动id
     */
    private ActivityInfo activityInfo;

    /**
     * 战令重置时间
     */
    private int tokenResetTime;

    private int maxOrder;

    public int getTokenResetTime() {
        return tokenResetTime;
    }

    public void setTokenResetTime(int tokenResetTime) {
        this.tokenResetTime = tokenResetTime;
    }

    public void setCricketFightForcePro(CricketFightForcePro cricketFightForcePro) {
        this.cricketFightForcePro = cricketFightForcePro;
    }

    public void setCricketConfigMap(Map<Integer, CricketConfig> cricketConfigMap) {
        this.cricketConfigMap = cricketConfigMap;
    }

    public void setCricketUpConfigMap(Map<Integer, CricketUpConfig> cricketUpConfigMap) {
        this.cricketUpConfigMap = cricketUpConfigMap;
    }

    public void setCricketScenceConfigMap(Map<Integer, CricketScenceConfig> cricketScenceConfigMap) {
        this.cricketScenceConfigMap = cricketScenceConfigMap;
    }

    public void setCricketCourtConfigMap(Map<Integer, CricketCourtConfig> cricketCourtConfigMap) {
        this.cricketCourtConfigMap = cricketCourtConfigMap;
    }

    public void setCricketFightScoreAttack(List<Integer> cricketFightScoreAttack) {
        this.cricketFightScoreAttack = cricketFightScoreAttack;
    }

    public void setCricketFightScoreDefend(List<Integer> cricketFightScoreDefend) {
        this.cricketFightScoreDefend = cricketFightScoreDefend;
    }

    public void setActivityInfo(ActivityInfo activityInfo) {
        this.activityInfo = activityInfo;
    }

    public void setMaxOrder(int maxOrder) {
        this.maxOrder = maxOrder;
    }

    public Map<Integer, CricketConfig> getCricketConfigMap() {
        return cricketConfigMap;
    }

    public Map<Integer, CricketUpConfig> getCricketUpConfigMap() {
        return cricketUpConfigMap;
    }

    public Map<Integer, CricketScenceConfig> getCricketScenceConfigMap() {
        return cricketScenceConfigMap;
    }

    public Map<Integer, CricketCourtConfig> getCricketCourtConfigMap() {
        return cricketCourtConfigMap;
    }

    public List<Integer> getCricketFightScoreAttack() {
        return cricketFightScoreAttack;
    }

    public List<Integer> getCricketFightScoreDefend() {
        return cricketFightScoreDefend;
    }

    public ActivityInfo getActivityInfo() {
        return activityInfo;
    }

    public int getMaxOrder() {
        return maxOrder;
    }

    public void initMaxOrder() {
        cricketScenceConfigMap.values().forEach(scenceConfig -> maxOrder = Math.max(maxOrder, scenceConfig.getOrder()));
    }


    public CricketConfig getCricketConfig(int cricketId) {
        return cricketConfigMap.get(cricketId);
    }

    public CricketScenceConfig getCricketScenceConfig(int scenceId) {
        return cricketScenceConfigMap.get(scenceId);
    }

    public CricketFightForcePro getCricketFightForcePro() {
        return cricketFightForcePro;
    }

    public CricketUpConfig getCricketUpConfig(int cricketUpId) {
        return cricketUpConfigMap.get(cricketUpId);
    }

    public CricketCourtConfig getCricketCourtConfig(int courtId) {
        return cricketCourtConfigMap.get(courtId);
    }

    public CricketCourtConfig getCricketCourtConfigByType(int type) {
        for (CricketCourtConfig courtConfig : cricketCourtConfigMap.values()) {
            if (courtConfig.getCourtType() == type) {
                return courtConfig;
            }
        }
        return null;
    }


    /**
     * 获取下一个开放的赛场
     *
     * @param courtId 当前赛场id
     */
    public CricketCourtConfig getNextOpenCourtConfig(int courtId) {
        CricketCourtConfig curConfig = getCricketCourtConfig(courtId);
        int courtType = curConfig.getCourtType();
        // 获取下一个赛场
        CricketCourtConfig nextConfig = getCricketCourtConfigByType(courtType + 1);
        if (nextConfig != null) {
            if (nextConfig.getCourtState() == 0) {
                // 再获取下一个
                return getNextOpenCourtConfig(nextConfig.getCourtId());
            } else {
                return nextConfig;
            }
        } else {
            return null;
        }
    }


    /**
     * 获取上一个开放的赛场
     *
     * @param courtId 当前赛场id
     */
    public CricketCourtConfig getPreOpenCourtConfig(int courtId) {
        CricketCourtConfig curConfig = getCricketCourtConfig(courtId);
        if (curConfig == null) {
            return null;
        }
        int courtType = curConfig.getCourtType();
        // 获取前一个赛场
        CricketCourtConfig preConfig = getCricketCourtConfigByType(courtType - 1);
        if (preConfig != null) {
            if (preConfig.getCourtState() == 0) {
                // 再获取前一个
                return getPreOpenCourtConfig(preConfig.getCourtId());
            } else {
                return preConfig;
            }
        } else {
            return null;
        }
    }


    /**
     * 获取初始的赛场(最小开放)
     */
    public CricketCourtConfig getInitCourtConfig() {
        int minType = Integer.MAX_VALUE;
        int courtId = -1;
        for (CricketCourtConfig courtConfig : getCricketCourtConfigMap().values()) {
            if (courtConfig.getCourtState() == 0) {
                continue;
            }
            if (courtConfig.getCourtType() < minType) {
                minType = courtConfig.getCourtType();
                courtId = courtConfig.getCourtId();
            }
        }
        return getCricketCourtConfig(courtId);
    }

    /**
     * 获取宗师赛场(最大开放)
     */
    public CricketCourtConfig getMaxCourtConfig() {
        int maxType = Integer.MIN_VALUE;
        int courtId = -1;
        for (CricketCourtConfig courtConfig : getCricketCourtConfigMap().values()) {
            if (courtConfig.getCourtState() == 0) {
                continue;
            }
            if (courtConfig.getCourtType() > maxType) {
                maxType = courtConfig.getCourtType();
                courtId = courtConfig.getCourtId();
            }
        }
        return getCricketCourtConfig(courtId);
    }

    /**
     * 获取晋级人数
     * 向上取整
     * @param courtId  赛场id
     * @param groupNum 分组人数
     */
    public int getUpCnt(int courtId, int groupNum) {
        CricketCourtConfig courtConfig = getCricketCourtConfig(courtId);
        if (courtConfig == null) {
            return 0;
        }
        if (groupNum == 0) {
            return 0;
        }
        // 获取晋级千分比
        int upNumRadio = courtConfig.getUpNum();
        // 计算晋级人数
        int upNum = BigDecimal.valueOf(groupNum).multiply(BigDecimal.valueOf(upNumRadio)).divide(BigDecimal.valueOf(1000), 0, RoundingMode.UP).intValue();
        return Math.max(0, Math.min(upNum, groupNum));
    }

    /**
     * 获取保留人数
     * 向上取整
     * @param courtId  赛场id
     * @param groupNum 分组人数
     */
    public int getKeepCnt(int courtId, int groupNum) {
        CricketCourtConfig courtConfig = getCricketCourtConfig(courtId);
        if (courtConfig == null) {
            return 0;
        }
        // 扣除晋级人数
        if (groupNum == 0) {
            return 0;
        }
        // 获取保留千分比
        int keepNumRadio = courtConfig.getKeepNum();
        // 计算保留人数
        int keepNum = BigDecimal.valueOf(groupNum).multiply(BigDecimal.valueOf(keepNumRadio)).divide(BigDecimal.valueOf(1000), 0, RoundingMode.UP).intValue();
        return Math.max(0, Math.min(keepNum, groupNum));
    }

    /**
     * 获取淘汰人数
     *
     * @param courtId  赛场id
     * @param groupNum 分组人数
     */
    public int getDownCnt(int courtId, int groupNum) {
        CricketCourtConfig courtConfig = getCricketCourtConfig(courtId);
        if (courtConfig == null) {
            return 0;
        }
        return Math.max(0, groupNum - getUpCnt(courtId, groupNum) - getKeepCnt(courtId, groupNum));
    }


}
